package com.hjx.windowtool.imgproc;

import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacpp.opencv_core.Mat;
import org.bytedeco.javacpp.opencv_core.Size;
import org.bytedeco.javacpp.opencv_dnn;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter.ToMat;
import org.eclipse.swt.graphics.RGB;

import com.hjx.windowtool.util.ImgUtil;
import com.sun.imageio.plugins.common.ImageUtil;

import static org.bytedeco.javacpp.opencv_imgcodecs.*;
import static org.bytedeco.javacpp.opencv_imgproc.*;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;

import javax.imageio.ImageIO;

import org.bytedeco.javacpp.opencv_core.Rect;


public class ImageProcTest {
	public static void main(String[] args) throws Exception {
			long currentTimeMillis = System.currentTimeMillis();
			Mat src = imread("e:\\123.jpg");
			IplImage image = cvvLoadImage("e:\\123.jpg");
//			System.out.println(src.empty());
//			Mat dst = new Mat();
//			//		System.out.println(dst.rows());
//			resize(src, dst, new Size(255, 255));
//			imwrite("E:\\1_1.PNG", dst);
			System.out.println(System.currentTimeMillis() - currentTimeMillis);
//			
//			Mat img = new Mat(src,new Rect(500, 256, 500, 300));
			Mat roi = src.apply(new Rect(700, 200, 255, 255));
////			dst.copyTo(roi);
//			Mat temp = roi.clone();
//			GaussianBlur(temp, temp, new Size(255,255), 9);
//			temp.copyTo(roi);
//			imwrite("E:\\1_11.PNG", src);
//			src.release();
//			dst.release();
//			Java2DFrameConverter java2DFrameConverter=new Java2DFrameConverter();
//
////			IplImage matImage = new IplImage(src);
//			OpenCVFrameConverter.ToIplImage toIpImage=new OpenCVFrameConverter.ToIplImage();
//			BufferedImage bufferedImage = java2DFrameConverter.convert(toIpImage.convert(src));
//			BufferedImage maskImage = ImgUtil.maskImage(3, bufferedImage, 700, 200, 255, 255);
//			Mat convertToMat = toIpImage.convertToMat(java2DFrameConverter.convert(maskImage));
//			imwrite("E:\\1_11.PNG", convertToMat);
			
			GaussianBlur(roi, roi, new Size(roi.rows(),roi.cols()), 5);
//			maskCenter(roi,4);
			imwrite("E:\\1_111.PNG", src);
			src.release();
			
	}

	/**
	 * @param roi
	 * @return
	 */
	public static void maskCenter(Mat roi,int size) {
		Mat temp = roi.clone();
		for (int i = size; i < roi.rows()-size; i+=size*2) {
			for (int j = size; j < roi.cols()-size; j+=size*2) {
//					Color red = Color.RED;
//					new RGB(red, green, blue)
				BytePointer s = temp.ptr(i, j);
				byte a = s.get(0);
				byte b = s.get(1);
				byte c = s.get(2);
				for (int k = i-size; k < i+size; k++) {
					for (int k2 = j-size; k2 < j+size; k2++) {
						BytePointer t = roi.ptr(k, k2);
						t.put(0, a);
						t.put(1, b);
						t.put(2, c);
					}
				}
			}
		}
		temp.release();
	}
	public static void maskFirst(Mat roi,int size) {
		Mat temp = roi.clone();
		for (int i = 0; i < roi.rows(); i+=size+1) {
			for (int j = 0; j < roi.cols(); j+=size+1) {
//					Color red = Color.RED;
//					new RGB(red, green, blue)
				BytePointer s = temp.ptr(i, j);
				byte a = s.get(0);
				byte b = s.get(1);
				byte c = s.get(2);
				for (int k = i; k < Math.min(i+size, roi.rows()); k++) {
					for (int k2 = j; k2 < Math.min(j+size, roi.cols()); k2++) {
						BytePointer t = roi.ptr(k, k2);
						t.put(0, a);
						t.put(1, b);
						t.put(2, c);
					}
				}
			}
		}
		temp.release();
	}
	public static void maskAver(Mat roi,int size) {
		Mat temp = roi.clone();
		for (int i = size; i < roi.rows()-size; i+=size*2) {
			for (int j = size; j < roi.cols()-size; j+=size*2) {
//					Color red = Color.RED;
//					new RGB(red, green, blue)
				BytePointer s = temp.ptr(i, j);
				byte a = s.get(0);
				byte b = s.get(1);
				byte c = s.get(2);
				long l=0;
				int length=1;
				l+=Integer.toUnsignedLong(new Color(c&0xff,b&0xff,a&0xff).getRGB());
				for (int k = i-size; k < i+size; k++) {
					for (int k2 = j-size; k2 < j+size; k2++) {
						BytePointer t = roi.ptr(k, k2);
						l+=Integer.toUnsignedLong(new Color(t.get(2)&0xff,t.get(1)&0xff,t.get(0)&0xff).getRGB());
						length++;
					}
				}
				Color color = new Color((int) (l/length));
				for (int k = i-size; k < i+size; k++) {
					for (int k2 = j-size; k2 < j+size; k2++) {
						BytePointer t = roi.ptr(k, k2);
						t.put(0, (byte) color.getBlue());
						t.put(1, (byte) color.getGreen());
						t.put(2, (byte) color.getRed());
					}
				}
			}
		}
		temp.release();
	}
	public static Mat combine(Mat mat1,Mat mat2,boolean left) {
		Mat mat=null;
		if (left) {
			mat=new Mat(Math.max(mat1.rows(), mat2.rows()), mat1.cols()+mat2.cols(), mat1.type());
			Mat roi1 = mat.apply(new Rect(0, 0, mat1.cols(), mat1.rows()));
			mat1.copyTo(roi1);
			Mat roi2 = mat.apply(new Rect(mat1.cols(), 0, mat2.cols(), mat2.rows()));
			mat2.copyTo(roi2);
		}else {
			mat=new Mat(mat1.rows()+mat2.rows(), Math.max(mat1.cols(),mat2.cols()), mat1.type());
			Mat roi1 = mat.apply(new Rect(0, 0, mat1.cols(), mat1.rows()));
			mat1.copyTo(roi1);
			Mat roi2 = mat.apply(new Rect(0, mat1.rows(), mat2.cols(), mat2.rows()));
			mat2.copyTo(roi2);
		}
		mat1.release();
		mat2.release();
		return mat;
	}
}
